package com.maiji.cloud.microservice;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.login.UserInfo;
import com.maiji.cloud.response.login.UserInfoResData;
import com.maiji.cloud.service.AppUserService;
import com.maiji.cloud.service.UserService;
import com.maiji.cloud.utils.RedisUtil;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userInfo/")
public class UserInfoController {

    @Autowired
    private RedisTemplate<String, Object> maiji1RedisTemplate;
    @Autowired
    private RedisTemplate<String, Object> maiji2RedisTemplate;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private UserService userService;

    @PostMapping("getAppUserByXId")
    public AppUser getAppUserByXId (@RequestBody String xId) {
        EntityWrapper<AppUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("uu_id", xId);
        entityWrapper.or();
        entityWrapper.eq("open_id", xId);
        return appUserService.selectOne(entityWrapper);
    }

    @PostMapping("editAppUserById")
    public Boolean editAppUserById (@RequestBody AppUser appUser) {
        Boolean updateById = appUserService.updateById(appUser);
        if (updateById) {
            AppUser appUser1 = appUserService.selectById(appUser.getUuId());
            RedisUtil.setObject(maiji1RedisTemplate, "maijiToken:" + appUser1.getToken(), appUser1);
            if (maiji2RedisTemplate != null) RedisUtil.setObject(maiji2RedisTemplate, "maijiToken:" + appUser1.getToken(), appUser1);
        }
        return updateById;
    }

    @PostMapping("selectList")
    public List<UserInfo> selectList (@RequestBody Map<String, Object> map) {
        EntityWrapper<UserInfo> entityWrapper = new EntityWrapper<>();
        String nickName = (String) map.get("nickName");
        if (StringUtil.isNotBlank(nickName)) entityWrapper.like("nick_name", nickName);
        List<String> userIds = (List<String>) map.get("userIds");
        if (userIds != null && userIds.size() > 0) entityWrapper.in("user_id", userIds);
        return userService.selectList(entityWrapper);
    }

    /**
     *  根据用户ID[]获取用户列表
     * @param userIds
     * @return
     */
    @PostMapping("selectListByUserIds")
    public List<UserInfo> selectListByUserIds (@RequestBody List<String> userIds) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("userIds", userIds);
        return selectList(map);
    }

    /**
     *  根据用户名获取用户列表
     * @param nickName
     * @return
     */
    @PostMapping("selectListByNickName")
    public List<UserInfo> selectListByNickName (@RequestBody String nickName) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("nickName", nickName);
        return selectList(map);
    }

    /**
     *  根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("findUserInfoByUserId")
    public UserInfo findUserInfoByUserId (@RequestParam(name = "userId") String userId) {
       if (StringUtil.isBlank(userId)) return null;
        EntityWrapper<UserInfo> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("user_id", userId);
        return userService.selectOne(entityWrapper);
    }

    /**
     *  根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("findUserInfoResDataByUserId")
    public UserInfoResData findUserInfoResDataByUserId (@RequestParam(name = "userId") String userId) {
        UserInfo userInfo = findUserInfoByUserId(userId);
        UserInfoResData userInfoResData = new UserInfoResData();
        BeanUtils.copyProperties(userInfo, userInfoResData);
        return userInfoResData;
    }

}
